<?php 
namespace app\common\model;
class MemberAmountLog extends Common{
	protected $insert = ['addtime'=>SYS_TIME];
	protected function getTypeAttr($value, $data){
		return config('AMOUNT_TYPE.'.$data['typeid']);
	}
	protected function getPaynameAttr($value, $data){
		return config('AMOUNT_LOG_STATUS.'.$data['paytype']);
	}
	protected function getAddtimeAttr($value){
		return date('Y-m-d H:i:s', $value);
	}
	protected function getStatusnameAttr($value, $data){
		$type = [0 => '交易进行中',1 => '交易成功', 2 => '交易失败'];
		return $type[$data['status']];
	}
	protected function getNicknameAttr($value, $data){
		$info = model("AdminUser")->get_data(['item_id'=>$data['userid']]);
		return $info['nickname'] ? $info['nickname'] : $info['phone'];
	}
	/**
	 * 记录资金变动日志
	 * @param [type] $userid      [用户id]
	 * @param [type] $typeid      [资金类型]
	 * @param [type] $paytype     [变动方式]-1:提现，1:充值，2:下单，4:积分兑换，5:退款，6:冻结资金，7:解冻资金 10:后台管理资金
	 * @param [type] $price       [变动金额]
	 * @param [type] $description [变动说明]
	 */
	public function add($data, $paytype, $status=1, $adminid=0, $typeid=1){
		if($data['price'] == 0) return false;
		$field = '';
		if ($status == 1) {
			switch($typeid){
				case 1:
					$field = 'amount';//红豆
					break;
				case 2:
					$field = 'jifen';//积分
					break;
			} 
		}
		if ($field) {
			model("AdminUser")->where('userid', $data['userid'])->setInc($field,$data['price']);
		}
		$datas = [
			'userid' => $data['userid'],
			'ordersn' => isset($data['ordersn']) ? $data['ordersn'] : '',
			'typeid' => $typeid,
			'paytype' => $paytype,
			'price' => $data['price'],
			'description' => isset($data['description']) && $data['description'] ? $data['description'] : config("AMOUNT_LOG_STATUS.".$paytype),
			'status' => $paytype == -1 ? $status : 1,
			'adminid' => $adminid,
			'addtime' => SYS_TIME,
		];
		return $this->insert($datas);
	}
}

